package com.jcxy.controller;

import com.jcxy.VO.UserVo;
import com.jcxy.mapper.DeptMapper;
import com.jcxy.mapper.DictionaryMapper;
import com.jcxy.mapper.UserMapper;
import com.jcxy.pojo.Position;
import com.jcxy.pojo.User;
import com.jcxy.service.PositionService;
import com.jcxy.service.UserService;
import com.jcxy.util.ParamsDto;
import com.jcxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 登录登出使用的控制器
 */
@Controller
@RequestMapping("/loginController")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private PositionService positionService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 登录方法
     * 登录成功后将user对象以及一些数据字典的对象存储在session中
     * @param request
     * @return
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public ResponseData login(HttpServletRequest request,HttpServletResponse response){

        //将请求中的参数转换成合适的map集合
        Map dto = ParamsDto.getDto(request.getParameterMap());
        //调用业务逻辑层方法，获得登录user对象
        User user = userService.login(dto);
        //创建返还前端的对象
        ResponseData ok = ResponseData.ok();

        if (user != null){
            HttpSession session = request.getSession();
            //登录成功，保存用户在session中，使用用户的id唯一标识
            session.setAttribute("user",user);

            //查询数据字典，存放在session中
            //性别
            session.setAttribute("sex",dictionaryMapper.queryDictionary("sex"));
            //皮肤
            session.setAttribute("theme_skin",dictionaryMapper.queryDictionary("theme_skin"));
            //学历
            session.setAttribute("education",dictionaryMapper.queryDictionary("education"));
            //部门
            session.setAttribute("dept",dictionaryMapper.queryDept());
            //角色
            session.setAttribute("role",dictionaryMapper.queryRole());
            //考勤类型
            session.setAttribute("attendance_type",dictionaryMapper.queryDictionary("attendance_type"));
            //考勤状态
            session.setAttribute("attendance_status",dictionaryMapper.queryStatus("attendance_status"));
            //通知类型
            session.setAttribute("notify_type",dictionaryMapper.queryDictionary("notify_type"));
            //通知状态
            session.setAttribute("notify_status",dictionaryMapper.queryStatus("notify_status"));
            //流程状态
            session.setAttribute("process_status",dictionaryMapper.queryStatus("process_status"));
            //报销方式数据字典
            session.setAttribute("payment",dictionaryMapper.queryDictionary("payment"));
            //费用科目数据字典
            session.setAttribute("subject",dictionaryMapper.querySubject());
            //出差类型
            session.setAttribute("evection_type",dictionaryMapper.queryDictionary("evection_type"));
            //加班类型
            session.setAttribute("overtime_type",dictionaryMapper.queryDictionary("overtime_type"));
            //请假类型
            session.setAttribute("holiday_type",dictionaryMapper.queryDictionary("holiday_type"));
            //审核状态
            session.setAttribute("audit_status",dictionaryMapper.queryStatus("audit_status"));
        }
        else{
            //登录失败，设置状态码和信息
            ok.setMsg("用户名或密码错误，登录失败！");
            ok.setCode(500);
        }

        return ok;
    }

    /**
     * 登出方法
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/loginOut.do")
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //使session失效
        request.getSession().invalidate();
        //重定向
        response.sendRedirect(request.getContextPath()+"/login.html");
    }

    /**
     * 通过部门id，查询部门职位
     * @param dept_id
     * @return
     */
    @RequestMapping("/getPositionListByDeptId.do")
    @ResponseBody
    public List<Position> getPositionListByDeptId(Integer dept_id){

        return positionService.queryPositionByDeptId(dept_id);
    }

    /**
     * 通过用户名查询用户是否存在
     * @param user_name
     * @return
     * @throws Exception
     */
    @RequestMapping("/queryUserByName.do")
    @ResponseBody
    public ResponseData queryUserByName(String user_name)throws Exception{

        UserVo userVo = userMapper.queryUserByName(user_name);
        ResponseData ok = ResponseData.ok();
        if (userVo != null){
            ok.setCode(500);
        }
        return ok;
    }

    /**
     * 更新登录用户信息
     * @param file
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateUser.do")
    public String updateUser(@RequestParam("img") MultipartFile file,HttpServletRequest request) throws Exception{

        Map dto = ParamsDto.getDto(request.getParameterMap());

        byte[] fileBytes = file.getBytes();
        if(fileBytes.length > 0){
            dto.put("img",fileBytes);
        }
        userService.updateUser(dto);

        //将操作成功码存放在请求中，用于前端显示提示框
        request.setAttribute("editOption","200");

        return "userInform";
    }

    /**
     * 获取人数前七的部门的部门名和人数
     * @return
     */
    @RequestMapping("/getDeptNum.do")
    @ResponseBody
    public ResponseData getDeptNum(){

        List<Map> data = deptMapper.queryDeptNum();
        ResponseData ok = ResponseData.ok();
        ok.setData(data);
        return ok;
    }
}
